class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0 % k,1);
        int count = 0,sum = 0,n = nums.length;
        for(int x : nums) {
            sum += x;
            int ret = (sum % k + k) % k;
            count += hash.getOrDefault(ret,0);
            hash.put(ret,hash.getOrDefault(ret,0) + 1);
        }
        return count;
    }
}